package com.eattravel.service.impl;

import com.eattravel.dto.PageInfoResult;
import com.eattravel.mapper.ShoppingCartMapper;
import com.eattravel.pojo.Commodity;
import com.eattravel.service.ShoppingCartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Override
    public List<Commodity> selectShoppingCart(String userId) {
        List<Commodity> commodities = shoppingCartMapper.selectShoppingCart(userId);
        return commodities;
    }

/*    @Override
    public PageInfoResult<Commodity> selectShoppingCart(PageInfoResult pageParameter, String userId) {
        //根据传来的分页信息查询数据
        PageHelper.startPage(pageParameter.getCurrPage(), pageParameter.getPageSize());
        List<Commodity> commodities = shoppingCartMapper.selectShoppingCart(userId);
        //将查询的数据装入到pageinfo
        PageInfo<Commodity> pageInfo = new PageInfo<Commodity>(commodities);
        // 查询的当前页数据信息
        List<Commodity> empList = pageInfo.getList();
        // 获取查询的总条数
        pageParameter.setTotalSize(pageInfo.getTotal());
        pageParameter.setDataList(empList);
        return pageParameter;
    }*/

    @Override
    public int deleteCommodity(String userId, String commodityId) {
        int rs = shoppingCartMapper.deleteCommodity(userId, commodityId);
        return rs;
    }

    @Override
    public int addToColl(String userId, String commodityId) {
        //先查一下收藏中是否存在这个商品
        int rs = shoppingCartMapper.selectSimpleColl(userId, commodityId);
        if (rs > 0) {
            //如果收藏中已经有了这个商品就返回1
            return 1;
        }
        //如果收藏中没有了这个商品就插入这个   返回0
        int rs1 = shoppingCartMapper.addToColl(userId, commodityId);
        return 0;
    }

    @Override
    public int updateCommodityNum(String userId, String commodityId, int num) {
        int rs = shoppingCartMapper.updateCommodityNum(userId, commodityId, num);
        return rs;
    }
}
